﻿using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using Cvk.Banner;

namespace BatDongSan.Manager
{
    public partial class ManBanner : System.Web.UI.Page
    {
        public static int _idc;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                _idc = Convert.ToInt32(Request.QueryString["idc"]);
                LoadBanner();
            }
        }
        public void LoadBanner()
        {
            try
            {
                List<Cvk_Banner> list = new List<Cvk_Banner>();
                if (_idc > 0) list = BannerBll.claBanner.GetList(_idc, Utility.strConnect);
                else list = BannerBll.claBanner.GetList(Utility.strConnect);
                grvBanner.DataSource = list;
                grvBanner.DataBind();
            }
            catch { }
        }

        protected void grvBanner_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Cvk_Banner info = (Cvk_Banner)e.Row.DataItem;

                Literal ltrFileName = (Literal)e.Row.FindControl("ltrFileName");
                if (info.FileType == 1)
                {
                    ltrFileName.Text = "<img id=\"" + info.BannerId + "\" style=\"border-width: 0px; height:70px\" src=\"/Uploads/Banner/Img/" + info.FileName + "\"/>";
                }
                else if (info.FileType == 2)
                {
                    ltrFileName.Text = "<object id=\"" + info.BannerId + "\" height=\"70\" classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\" codebase=\"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,28,0\"> " +
                                        " <param name=\"allowScriptAccess\" value=\"sameDomain\"/> " +
                                        " <param name=\"movie\" value=\"/Uploads/Banner/flash/" + info.FileName + "\"/>" +
                                        " <param name=\"quality\" value=\"high\"/>" +
                                        " <param name=\"wmode\" value=\"transparent\"/>" +
                                        " <embed height=\"70\" src=\"/Uploads/Banner/flash/" + info.FileName + "\" allowscriptaccess=\"sameDomain\" wmode=\"transparent\" quality=\"high\" swliveconnect=\"true\" pluginspage=\"http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash\" type=\"application/x-shockwave-flash\" name=\"testing\"/>" +
                                    " </object>";
                }


                HyperLink hplName = (HyperLink)e.Row.FindControl("hplName");
                hplName.NavigateUrl = string.Format("~/Manager/ManBannerUpdate.aspx?idc={0}&id={1}", info.CategoryId, info.BannerId);

                TextBox txtPosition = (TextBox)e.Row.FindControl("txtPosition");
                txtPosition.Text = info.Position.ToString();

                LinkButton lnkbtActive = (LinkButton)e.Row.FindControl("lnkbtActive");
                if (Convert.ToBoolean(info.IsActive))
                {
                    lnkbtActive.Text = "#hiện";
                    lnkbtActive.ForeColor = System.Drawing.Color.Blue;
                }
                else { lnkbtActive.Text = "Ẩn"; lnkbtActive.ForeColor = System.Drawing.Color.Red; }
                lnkbtActive.Attributes.Add("id", info.BannerId.ToString());
                
                LinkButton lnkbtDelete = (LinkButton)e.Row.FindControl("lnkbtDelete");
                lnkbtDelete.OnClientClick = "return confirm('Bạn có chắc không?')";
                lnkbtDelete.Attributes.Add("id", info.BannerId.ToString());
            }
        }

        protected void lnkbtActive_Click(object sender, EventArgs e)
        {
            try
            {
                LinkButton lnkbtActive = (LinkButton)sender;
                int id = Convert.ToInt32(lnkbtActive.Attributes["id"]);
                if (id > 0)
                {
                    if (lnkbtActive.ForeColor == System.Drawing.Color.Red)
                    {
                        //False
                        if (BannerBll.claBanner.UpdateActive(id, true, Utility.strConnect))
                        {
                            lbStatus.Text = "Bạn đã xét Ẩn thành công";
                            LoadBanner();
                        }
                        else { lbStatus.Text = "Lỗi, Không thể thực hiện thao tác này."; }
                    }
                    else
                    {
                        //True
                        if (BannerBll.claBanner.UpdateActive(id, false, Utility.strConnect))
                        {
                            lbStatus.Text = "Bạn đã chọn Tin #Hiện thành công";
                            LoadBanner();
                        }
                        else { lbStatus.Text = "Lỗi, Không thể thực hiện thao tác này."; }
                    }
                }
            }
            catch { }
        }
        protected void lnkbtDelete_Click(object sender, EventArgs e)
        {
            try
            {
                LinkButton lnkbtDelete = (LinkButton)sender;
                int id = Convert.ToInt32(lnkbtDelete.Attributes["id"]);

                if (id > 0)
                {
                    if (BannerBll.claBanner.UpdateDeleted(id, true, Utility.strConnect))
                    {
                        lbStatus.Text = "Bạn xóa thành công";
                    }
                    else
                    {
                        lbStatus.Text = "Thất bại rồi bạn à";
                    }
                    LoadBanner();
                }
            }
            catch { }
        }
        protected void btnPosition_Click(object sender, EventArgs e)
        {
            try
            {
                foreach (GridViewRow row in grvBanner.Rows)
                {
                    try
                    {
                        Label lbId = (Label)row.FindControl("lblImgAlbumId");
                        int id = Convert.ToInt32(lbId.Text);
                        TextBox txt = (TextBox)row.FindControl("txtPosition");
                        int pos = Convert.ToInt32(txt.Text);
                        BannerBll.claBanner.UpdatePosition(id, pos, Utility.strConnect);
                    }
                    catch { }
                }
                LoadBanner();
            }
            catch (Exception ex)
            {
                lbStatus.Text = ex.ToString();
            }
        }
    }
}
